package com.qfedu.zf.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qfedu.zf.common.vo.R;
import com.qfedu.zf.index.entity.Listings;
import com.qfedu.zf.index.mapper.ListingsMapper;
import com.qfedu.zf.my.entity.Collect;
import com.qfedu.zf.my.mapper.CollectMapper;
import com.qfedu.zf.my.mapper.RentMapper;
import com.qfedu.zf.my.service.CollectService;
import com.qfedu.zf.my.vo.RentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    RentMapper rentMapper;
    @Autowired
    ListingsMapper listingsMapper;


    @Override
    public R collect(Integer uid) {
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("user_id",uid);
        List<Collect> collects = collectMapper.selectList(collectQueryWrapper);
        System.out.println(collects);
//        ArrayList<RentVo> collects1 = new ArrayList<>();
//        for (int i = 0; i < collects.size(); i++) {
//            Collect collect = collects.get(i);
//            Integer userId = collect.getUserId();
//            QueryWrapper<RentVo> rentQueryWrapper = new QueryWrapper<>();
//            rentQueryWrapper.eq("user_id",userId);
//            RentVo rentVo = rentMapper.selectOne(rentQueryWrapper);
//            collects1.add(rentVo);
//        }
        QueryWrapper<RentVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        List<RentVo> collects1 = rentMapper.selectList(queryWrapper);
        return R.ok(collects1);
    }

    @Override
    public R find(Integer listingId, Integer userId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("listing_id",listingId).eq("user_id",userId);
        List<Collect> collects = collectMapper.selectList(queryWrapper);
        if (collects.size() == 0){
            return R.fail("未收藏");
        }
        return R.ok();
    }

    @Override
    public R add(Integer listingId, Integer userId) {
        Collect collect = new Collect();
        collect.setRentId(listingId);
        collect.setListingId(listingId);
        collect.setUserId(userId);
        int insert = collectMapper.insert(collect);
        Listings listings = listingsMapper.selectById(listingId);
        if (listings != null) {
            if (listings.getCollect() == null){
                listings.setCollect(1);
            } else {
                listings.setCollect(listings.getCollect()+1);
            }
            int i = listingsMapper.updateById(listings);
            if (i < 0){
                return R.fail("收藏失败");
            }
        } else {
            return R.fail("收藏失败");
        }
        if (insert < 0){
            return R.fail("收藏失败");
        }

        //添加收藏
        RentVo rentVo = new RentVo();
        rentVo.setId(listingId);
        rentVo.setUserId(userId);
        rentVo.setPicture(listings.getCover());
        rentVo.setAddress(listings.getAddress());
        rentVo.setFloor(listings.getFloor());
        rentVo.setHouseType(listings.getHouseType());
//        rentVo.setHouseRent(Integer.parseInt (listings.getPrice().toString()));
        int insert1 = rentMapper.insert(rentVo);
        if (insert1 < 0){
            return R.fail("收藏失败");
        }
        return R.ok();
    }


}
